<?php


namespace App\Utils;


use Predis\Client;

class Cache
{
    protected static $redis = null;

    /**
     * @return Client
     */
    public static function getRedis()
    {
        if (is_null(self::$redis)) {
            self::$redis = new Client([
                'scheme' => 'tcp',
                'host' => 'redis',
                'port' => 6379
            ]);
//            self::$redis->select(3);
            Logger::getLogger('redis')->info("init");
        }
        return self::$redis;
    }


    public static function __callStatic($name, $arguments)
    {
        if (method_exists(self::class, $name)) {
            return (new self())->{$name}(...$arguments);
        }
        return self::getRedis()->{$name}(...$arguments);
    }

    protected function dispatch($key, $payload)
    {
        self::rpush($key, serialize($payload));
    }

    protected function dequeue($key)
    {
        $job = self::lpush($key);
        if ($job) {
            return unserialize($job);
        }
        return null;
    }

}